package com.ty.xiaoyu.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.ty.xiaoyu.entity.User;
import com.ty.xiaoyu.service.*;
import com.ty.xiaoyu.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
public class BaseController {

    @Autowired
    protected HomePageRotationChartService homePageRotationChartService;
    @Autowired
    protected TopicService topicService;
    @Autowired
    protected UserService userService;
    @Autowired
    protected ReplyService replyService;
    @Autowired
    protected ModuleService moduleService;
    @Autowired
    protected BrowsingHistoryService browsingHistoryService;
    @Autowired
    protected SearchHistoryService searchHistoryService;
    @Autowired
    protected CommentService commentService;
    @Autowired
    protected GreatService greatService;
    @Autowired
    protected NotifyService notifyService;
    @Autowired
    protected NotifyModuleService notifyModuleService;

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected User loginUser;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        String token = request.getHeader("Authorization");
        if (token == null) {
            this.loginUser = null;
            return;
        }
        DecodedJWT verify = JWTUtil.verify(token);
        User user = new User();
        user.setUserId(verify.getClaim("userId").asString());
        user.setUserName(verify.getClaim("userName").asString());
        log.info("当前用户：" + user);
        this.loginUser = user;
    }


}
